package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.huike.business.domain.TbBusiness;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.*;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.contract.domain.TbContract;
import com.huike.report.domain.vo.DataVo;
import org.apache.poi.ss.usermodel.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    
    @Autowired
    private ReportMapper reportMpper;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId= request.getDeptId();
        TbAssignRecord tbAssignRecord=new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public DataVo getAllclue(String beginCreateTime, String endCreateTime)  {

        List<TbClue> tbClues = reportMpper.selectTbClueList(beginCreateTime, endCreateTime);
        int clueNumbers = tbClues.size();
        List<TbBusiness> tbBusinesses = reportMpper.selectBusinessNumsList(beginCreateTime, endCreateTime);
        int businesses = tbBusinesses.size();
        List<TbContract> tbContracts = reportMpper.selectContractList(beginCreateTime, endCreateTime);
        int contracts = tbContracts.size();
        List<TbClue> effectiveClueList = reportMpper.selectEffectiveClueList(beginCreateTime, endCreateTime);
        DataVo dataVo = new DataVo();
        dataVo.setCluesNums(clueNumbers);
        dataVo.setBusinessNums(businesses);
        dataVo.setContractNums(contracts);
        dataVo.setEffectiveCluesNums(effectiveClueList.size());



        return dataVo;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }

        }
        return list;
    }

    @Override
    public List<Map<String, Object>> salesStatistic(IndexStatisticsVo request) {

        Integer all = clueMapper.countByClueCreateTime(request.getBeginCreateTime(), request.getEndCreateTime());

         List<Map<String, Object>> list = assignRecordMapper.getSalesStatistic(request);

        for (Map<String, Object> datum : list) {
            SysUser sysUser= sysUserMapper.selectUserByUserName((String) datum.get("create_by"));
            Long deptId = sysUser.getDeptId();
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
            BigDecimal radio = getRadio(all, (Long) datum.get("num"));
            datum.put("radio",radio);
        }
        return list;
    }


    private BigDecimal getRadio(Integer all,Long num) {
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
		BigDecimal numBigDecimal = new BigDecimal(num);
		BigDecimal allBigDecimal = new BigDecimal(all);
		BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
		return divide.multiply(new BigDecimal(100));
	}

}